from pyparsing import unichr


def strQ2B(ustring):
    """全角转半角"""
    rstring = ""
    for uchar in ustring:
        inside_code = ord(uchar)
        if inside_code == 12288:  # 全角空格直接转换
            inside_code = 32
        elif 65281 <= inside_code <= 65374:  # 全角字符（除空格）根据关系转化
            inside_code -= 65248

        rstring += unichr(inside_code)
    return rstring


path = '/data/corpus/hj/synonymous_800000.txt'
half_angle_data = '/data/corpus/hj/synonymous_half_angle.txt'


def read_write_file(path_a, f_a):
    f2 = open(f_a, 'w', encoding='utf-8')
    i = 0
    with open(path_a, 'r', encoding='utf-8') as f1:
        lines = f1.readlines()
        for line in lines:
            i += 1
            if i % 1000 == 0:
                print("执行到了第%d条" % i)
            f2.write(strQ2B(line))


if __name__ == '__main__':
    read_write_file(path, half_angle_data)
